package com.uphone.kingmall.activity.order;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.adapter.SendCompanyAdapter;
import com.uphone.kingmall.app.MyApplication;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.bean.SendCompanyBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.PopupUtil;
import com.uphone.kingmall.utils.ToastUtils;
import com.uphone.kingmall.view.SubmitButton;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : 孙增艳
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2019/2/26
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class ApplyForRefund2Activity extends BaseActivity {
    @BindView(R.id.tv_express_company)
    TextView tvExpressCompany;
    @BindView(R.id.tv_express_number)
    EditText tvExpressNumber;
    @BindView(R.id.tv_confirm)
    SubmitButton tvConfirm;
    private PopupUtil popupFilter;
    private List<SendCompanyBean.SendCosBean> companyList;
    int companyId = -1;
    private String company;
    private String comno;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_apply_for_refund2;
    }

    @Override
    public void initView() {
        tvConfirm.setRelaViews(tvExpressCompany, tvExpressNumber);
    }

    @Override
    public void initData() {
        OkGoUtils.normalRequest(MyUrl.getSendCompany, null, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    SendCompanyBean bean = GsonUtils.getGson().fromJson(body, SendCompanyBean.class);
                    companyList = bean.getSendCos();
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_express_company, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_express_company:
                showPopup();
                break;
            case R.id.tv_confirm:
                if (TextUtils.isEmpty(company) || TextUtils.isEmpty(comno)) {
                    ToastUtils.showShortToast(this, "请选择物流公司");
                    return;
                }
                if (CommonUtil.checkViewEmpty("请填写物流单号", tvExpressNumber)) {
                    return;
                }
                KeyboardUtils.hideSoftInput(this);
                Intent intent = new Intent();
                intent.putExtra("company", company + "");
                intent.putExtra("comNo", comno + "");
                intent.putExtra("number", tvExpressNumber.getText().toString().trim());
                setResult(10, intent);
                finish();
                break;
            default:
                break;
        }
    }

    private void showPopup() {
        if (companyList == null) {
            ToastUtils.showShortToast(this, "没有加载到店铺分类信息");
            return;
        }
        View inflate = View.inflate(this, R.layout.kingmall_popup_shop_type, null);
        TextView tv = inflate.findViewById(R.id.tv_name);
        tv.setText("物流公司");
        RecyclerView rv = inflate.findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
        final SendCompanyAdapter userShopTypeAdapter = new SendCompanyAdapter();
        rv.setAdapter(userShopTypeAdapter);
        userShopTypeAdapter.setNewData(companyList);
        if (companyId != -1) {
            userShopTypeAdapter.setSelectCatId(companyId);
        }
        popupFilter = PopupUtil.getInstance(MyApplication.mContext, inflate);
        popupFilter.showBottom();
        inflate.findViewById(R.id.tv_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userShopTypeAdapter != null && userShopTypeAdapter.getSelectPosition() != -1) {
                    company = companyList.get(userShopTypeAdapter.getSelectPosition()).getCompanyName() + "";
                    tvExpressCompany.setText(company);
                    companyId = companyList.get(userShopTypeAdapter.getSelectPosition()).getId();
                    comno = companyList.get(userShopTypeAdapter.getSelectPosition()).getCompanyCode();
                    popupFilter.dismiss();
                } else {
                    com.blankj.utilcode.util.ToastUtils.showShort("请选择物流公司");
                }
            }
        });
        popupFilter.setDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (popupFilter != null) {
                    popupFilter.release();
                }
            }
        });

        userShopTypeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                userShopTypeAdapter.setSelectCatId(companyList.get(position).getId());
            }
        });
    }


    @Override
    protected void onStop() {
        super.onStop();
        if (popupFilter != null) {
            popupFilter.release();
        }
    }
}
